<template>
  <div>
    <!--
      必要的数据
      action 必选项作为上传地址, 我们使用云服务, 这里放 # 敷衍一下
      fileList 管理图片的数组

      控制样式
      list-type 设置照片墙样式
      class 动态类名, 根据当前文件列表长度控制上传按钮显示

      钩子
      on-preview 预览
      on-change 修改
      on-remove 删除

      上传功能相关
      before-upload 上传前校验
      http-request 覆盖 action 默认的上传行为
     -->
    <el-upload
      action="#"
      :file-list="fileList"

      :class="{disable: fileList.length>0}"
      list-type="picture-card"

      :on-preview="onPreview"
      :on-remove="onRemove"
      :on-change="onChange"

      :before-upload="beforeUpload"
      :http-request="upload"
    >
      <i class="el-icon-plus" />
    </el-upload>

    <el-progress
      v-if="isShowPercent"
      :text-inside="true"
      :stroke-width="26"
      :percentage="percent"
      style="width: 146px;"
    />

    <el-dialog title="预览" :visible="isShowDialog" @close="isShowDialog = false">
      <el-row type="flex" justify="center">
        <img :src="previewURL" alt="">
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
// 引入库
import COS from 'cos-js-sdk-v5'
// 创建实例
const cos = new COS({
  SecretId: 'AKID0rVv62jBUhkX4KLH7g4FAsY7RZT7VeW8',
  SecretKey: 'SfcJn58wVvLdkcGfs32nJcjcGNK5dx4y'
})
export default {
  data() {
    return {
      fileList: [],
      isShowDialog: false,
      previewURL: '',
      percent: 0,
      isShowPercent: false
    }
  },
  methods: {
    onPreview(file) {
      this.previewURL = file.url
      this.isShowDialog = true
    },
    onRemove(file, newFileList) {
      this.fileList = newFileList
    },
    onChange(file, newFileList) {
      this.fileList = newFileList
    },
    beforeUpload(file) {
      // 校验大小
      const maxSize = 2 * 1024 * 1024
      if (file.size > maxSize) {
        this.$message.warning('图片大小不能超过 2M')
        return false
      }
      // 校验格式
      const whiteList = ['image/jpeg', 'image/png']
      if (!whiteList.includes(file.type)) {
        this.$message.warning('暂时只支持 jpeg / png 格式')
        return false
      }
    },
    upload(params) {
      console.log(params)

      this.isShowPercent = true

      cos.putObject({
        // 储存桶名
        Bucket: '69hr-1300310660',
        // 区域标志
        Region: 'ap-nanjing',
        // 文件名
        Key: params.file.name,
        // 固定写法, 标准储存的意思
        StorageClass: 'STANDARD',
        // 文件对象本身
        Body: params.file,
        onProgress: (progressData) => {
          console.log(JSON.stringify(progressData))
          this.percent = Math.round(progressData.percent * 100)
        }
      }, (err, data) => {
        console.log(err || data)
        // 这是腾讯云上传结束的回调
        // 如果成功, 要手动通知elementUI上传完毕
        // 修改 fileList 里面的图片url
        this.fileList[0].url = 'http://' + data.Location
        this.fileList[0].status = 'success'

        setTimeout(() => {
          this.isShowPercent = false
        }, 1000)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.disable {
  // /deep/ .el-upload--picture-card {
  ::v-deep .el-upload--picture-card {
    display: none;
  }
}
</style>

<style lang="scss">
img {
  object-fit: cover
}
</style>
